<?php

namespace App\Http\Controllers\BG;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

use Cache;
use Validator;
use Carbon\Carbon;

use  App\Models\WeixinFriendList;
use  App\Models\WeixinGroup;
use  App\Models\User;
use  App\Models\Weixin;

class FriendController extends Controller
{
    public function list(Request $request)
    {
        $input = Input::only('page', 'limit','own_str','friend_str','group_id','user_id','sex');
        $validator = Validator::make($input, [        
            'page'          => 'required|string',
            'limit'         => 'required|string',
            'own_str'       => 'nullable|string',
            'friend_str'    => 'nullable|string',
            'group_id'      => 'nullable|string',
            'user_id'       => 'nullable|string',
            'sex'           => 'nullable|string',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $friend = WeixinFriendList::where('id','>',0);
        $page = $input['page'];
        $limit = $input['limit'];
        if(isset($input['own_str'])){
            $o_weixin = Weixin::where('id',$input['own_str'])->first();
            $friend->where('weixin_uid',$o_weixin['uid']); 
        }
        if(isset($input['friend_str'])){
            $friend->where('alias','like' , '%'.$input['friend_str'].'%')->orwhere('nickname','like' , '%'.$input['friend_str'].'%');
        }
        if(isset($input['group_id'])){
            $friend->where('weixin_group_id',$input['group_id']); 
        }
        if(isset($input['user_id'])){
            $friend->where('user_id',$input['user_id']); 
        }
        if(isset($input['sex'])){
            $friend->where('sex',$input['sex']); 
        }
        $count = $friend->count();
        $data = $friend->select('id','weixin_uid','alias as friend_uid','uid','nickname as friend_nickname','small_head_url as friend_small_head_url','sex as friend_sex','user_id','weixin_group_id','com_remark','province','city')
            ->offset(($page-1)*$limit)
            ->limit($limit)
            ->get();
        $data = $this->formatFriendData($data);
        $fin['data'] = $data;
        $fin['count'] = $count;

        return $this->returnOk($fin);
    }

    protected function formatFriendData($data)
    {
        $uids = [];
        $group_ids = [];
        $user_ids = [];
        foreach ($data as $key => $value) {
            array_push($uids,$value['weixin_uid']);
            array_push($group_ids,$value['weixin_group_id']);
            array_push($user_ids,$value['user_id']);
        }
        $weixinList = Weixin::whereIn('uid',$uids)->select('uid as weixin_uid','alias as weixin_alias','nickname as weixin_nickname','small_head_url as weixin_small_head_url')->get();
        // dd($weixinList);
        // exit;
        $groupList = WeixinGroup::whereIn('id',$group_ids)->select('name as group_name','id as group_id')->get();
        $userList = User::whereIn('id',$user_ids)->select('name as user_name','id as user_id')->get();
        foreach ($data as $key => $value) {
            // $data[$key]['weixin_uid'] = '';
            $data[$key]['weixin_nickname'] = '';
            $data[$key]['weixin_small_head_url'] = '';
            $data[$key]['weixin_alias'] = '';
            $data[$key]['group_id'] = '';
            $data[$key]['group_name'] = '';
            $data[$key]['user_name'] = '';
            foreach ($weixinList as $wkey => $wvalue) {
                if($value['weixin_uid']== $wvalue['weixin_uid']){
                    $data[$key]['weixin_uid'] = $wvalue['weixin_uid'];
                    $data[$key]['weixin_nickname'] = $wvalue['weixin_nickname'];
                    $data[$key]['weixin_small_head_url'] = $wvalue['weixin_small_head_url'];
                    $data[$key]['weixin_alias'] = $wvalue['weixin_alias'];
                }
            }
            foreach ($groupList as $gkey => $gvalue) {
                if($value['weixin_group_id']==$gvalue['group_id']){
                    $data[$key]['group_id'] = $gvalue['group_id'];
                    $data[$key]['group_name'] = $gvalue['group_name'];
                }
            }
            foreach ($userList as $ukey => $uvalue) {
                if($value['user_id']==$uvalue['user_id']){
                    $data[$key]['user_id'] = $uvalue['user_id'];
                    $data[$key]['user_name'] = $uvalue['user_name'];
                }
            }
            unset($data[$key]['weixin_group_id']);
        }
        return $data;
    }

    public function setGroup(Request $request)
    {
        $input = Input::only('ids', 'group_id');
        $validator = Validator::make($input, [        
            'ids'      => 'required',
            'group_id'         => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        if($input['group_id']){
            $group = WeixinGroup::where('id',$input['group_id'])->first();
            if($group){
                $data['weixin_group_id'] = $input['group_id'];
                WeixinFriendList::whereIn('id',$input['ids'])->update($data);
                return $this->returnOk();
            }else{
                return $this->returnError([],409,'分组不存在');
            }
        }else{
            $data['weixin_group_id'] = 0;
            WeixinFriendList::whereIn('id',$input['ids'])->update($data);
            return $this->returnOk();
        }
        
    }

    public function setUser()
    {
        $input = Input::only('ids',  'user_id');
        $validator = Validator::make($input, [        
            'ids'      => 'required',
            'user_id'         => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        if($input['user_id']){
            $phone = User::where('id',$input['user_id'])->first();
            if($phone){
                $data['user_id'] = $input['user_id'];
                WeixinFriendList::whereIn('id',$input['ids'])->update($data);
                return $this->returnOk();
            }else{
                return $this->returnError([],409,'用户不存在');
            }
        }
        $data['user_id'] = 0;
        WeixinFriendList::whereIn('id',$input['ids'])->update($data);
        return $this->returnOk();
    }
}